package com.wxapp.admin.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wxapp.admin.api.pojo.entity.CompanyDeptDO;
import com.wxapp.admin.api.pojo.entity.DeptDO;
import com.wxapp.admin.api.pojo.vm.dept.PDeptDetailVm;
import com.wxapp.admin.api.pojo.vm.dept.PDeptTreeVm;
import com.wxapp.admin.api.pojo.vo.dept.QDeptSaveVo;
import com.wxapp.admin.api.pojo.vo.dept.QDeptTreeVo;
import com.wxapp.admin.api.pojo.vo.dept.QDeptUpdateVo;
import com.wxapp.admin.service.mapper.CompanyDeptMapper;
import com.wxapp.admin.service.mapper.DeptMapper;
import com.wxapp.admin.service.service.DeptService;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.check.CompareUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.common.util.menuTree.TreeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lgl
 * @Description 部门管理
 * @Datetime 2022/8/6 17:12
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class DeptServiceImpl extends IBaseService implements DeptService {
    private final DeptMapper deptMapper;
    private final CompanyDeptMapper companyDeptMapper;

    @Override
    public ResultVm<String> saveDept(UserVo userVo, QDeptSaveVo qDeptSaveVo) {
        LambdaQueryWrapper<DeptDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeptDO::getDeptName, qDeptSaveVo.getDeptName()).eq(DeptDO::getStatus, true);
        var count = deptMapper.selectCount(wrapper);
        if (count > 0) {
            return error("已经存在该部门名称的部门");
        }
        var deptDO = CopyUtil.copyObjectNew(qDeptSaveVo, DeptDO::new, null);
        deptMapper.insert(deptDO);
        CompanyDeptDO companyDeptDO = new CompanyDeptDO();
        companyDeptDO.setCompanyId(qDeptSaveVo.getCompanyId());
        companyDeptDO.setDeptId(deptDO.getId());
        companyDeptDO.setCreateBy(userVo.getId());
        companyDeptMapper.insert(companyDeptDO);
        return success("保存成功");
    }

    @Override
    public ResultVm<String> updateDept(UserVo userVo, QDeptUpdateVo qSaveVo) {
        var deptDO = deptMapper.selectById(qSaveVo.getId());
        if (CheckUtil.isNullObject(deptDO)) {
            return error("没有查到部门信息");
        }
        CopyUtil.copyObject(qSaveVo, deptDO, null);
        deptMapper.updateById(deptDO);
        if (CheckUtil.isNotNullObject(qSaveVo.getCompanyId())) {
            LambdaUpdateWrapper<CompanyDeptDO> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(CompanyDeptDO::getDeptId, qSaveVo.getId()).eq(CompanyDeptDO::getStatus, true).set(CompanyDeptDO::getStatus, false);
            companyDeptMapper.update(null, updateWrapper);
            CompanyDeptDO companyDeptDO = new CompanyDeptDO();
            companyDeptDO.setCompanyId(qSaveVo.getCompanyId());
            companyDeptDO.setDeptId(deptDO.getId());
            companyDeptDO.setCreateBy(userVo.getId());
            companyDeptMapper.insert(companyDeptDO);
        }
        return success("更新成功");
    }

    @Override
    public ResultVm<PDeptDetailVm> detailDept(String id) {
        var deptDO = deptMapper.selectById(id);
        if (CheckUtil.isNullObject(deptDO)) {
            return error("没有查到部门信息");
        }
        var pDetailVm = CopyUtil.copyObjectNew(deptDO, PDeptDetailVm::new, null);
        LambdaUpdateWrapper<CompanyDeptDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CompanyDeptDO::getDeptId, deptDO.getId()).eq(CompanyDeptDO::getStatus, true);
        var companyDeptDO = companyDeptMapper.selectFirst(wrapper);
        if (CheckUtil.isNullObject(companyDeptDO)){
            return error("没有查到公司信息");
        }
        pDetailVm.setCompanyId(companyDeptDO.getCompanyId());
        return success(pDetailVm);
    }

    @Override
    public ResultVm<List<PDeptTreeVm>> treeDept(QDeptTreeVo qSaveVo) {
        List<PDeptTreeVm> deptPage = deptMapper.selectDeptList(qSaveVo);
        if (CheckUtil.isNullList(deptPage)){
            return success(new ArrayList<>());
        }
        List<PDeptTreeVm> iBaseMenuTrees = TreeUtil.buildTree(deptPage,null);
        return success(iBaseMenuTrees);
    }

    @Override
    public ResultVm<String> deleteDept(String id) {
        var deptDO = deptMapper.selectById(id);
        if (CheckUtil.isNullObject(deptDO)) {
            return error("没有查到部门信息");
        }
        LambdaQueryWrapper<DeptDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeptDO::getStatus, true).eq(DeptDO::getPid, id);
        var aLong = deptMapper.selectCount(wrapper);
        if (CompareUtil.oneBigLong(aLong, 0L)) {
            return error("该部门下有关联子部门，请先删除子部门！");
        }
        deptDO.setStatus(false);
        deptMapper.updateById(deptDO);
        LambdaUpdateWrapper<CompanyDeptDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CompanyDeptDO::getDeptId, id).eq(CompanyDeptDO::getStatus, true).set(CompanyDeptDO::getStatus, false);
        companyDeptMapper.update(null, updateWrapper);
        return success("删除成功");
    }
}
